package com.manager.schedule.controller;

import com.manager.common.strategy.StrategyManager;
import com.manager.common.vo.PageVO;
import com.manager.common.vo.ResultVO;
import com.manager.model.SchedulingLogs;
import com.manager.model.SchedulingStrategy;
import com.manager.model.SchedulingStrategyDockerContainer;
import com.manager.schedule.service.SchedulingService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/scheduling")
public class SchedulingController {

    @Autowired
    private SchedulingService schedulingService;

    @RequestMapping("/getPage")
    public PageVO getPage(SchedulingStrategy model) throws Exception{
        return schedulingService.getSchedulePage(model);
    }

    @RequestMapping("/getScheduleLogPage")
    public PageVO getScheduleLogPage(SchedulingLogs model) throws Exception{
        return schedulingService.getScheduleLogPage(model);
    }

    @RequestMapping("/addOrUpdate")
    public ResultVO addOrUpdate(SchedulingStrategy model) throws Exception{
        try{
            if(StringUtils.isEmpty(model.getId())){
                schedulingService.addSchedule(model);
            }else {
                schedulingService.updateSchedule(model);
            }
            SchedulingStrategy schedulingStrategy = schedulingService.getSchedulingStrategy(model.getId());
            StrategyManager.setSchedulingStrategy(schedulingStrategy.getId(),schedulingStrategy);
            return ResultVO.success();
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultVO.failed();
    }

    @RequestMapping("/delete")
    public ResultVO delete(SchedulingStrategy model) throws Exception{
        try{
            schedulingService.deleteSchedule(model);
            StrategyManager.removeSchedulingStrategy(model.getId());
            return ResultVO.success();
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultVO.failed();
    }

    @RequestMapping("/stopScheduling")
    public ResultVO stopScheduling(SchedulingStrategy model) throws Exception{
        try{
            schedulingService.stopScheduling(model.getId());
            SchedulingStrategy schedulingStrategy = schedulingService.getSchedulingStrategy(model.getId());
            StrategyManager.setSchedulingStrategy(schedulingStrategy.getId(),schedulingStrategy);
            return ResultVO.success();
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultVO.failed();
    }

    @RequestMapping("/startScheduling")
    public ResultVO startScheduling(SchedulingStrategy model) throws Exception{
        try{
            schedulingService.startScheduling(model.getId());
            SchedulingStrategy schedulingStrategy = schedulingService.getSchedulingStrategy(model.getId());
            StrategyManager.setSchedulingStrategy(schedulingStrategy.getId(),schedulingStrategy);
            return ResultVO.success();
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultVO.failed();
    }
}
